mod model;
use log::info;
use warp::{Filter};
use log4rs::init_file;
use signal_hook::{iterator::Signals,consts::SIGTERM};

#[tokio::main]
async fn main() -> Result<(),Box<dyn std::error::Error>>{
    // data里面包含的测试数据如下
    // 9df32dd1-5ec3-4deb-b900-fd909c613a1f:hello world
    // 10578ce1-f23d-4e13-856c-fb5e57110184:hello world
    // 0760a3d6-88ad-4e8d-ad24-c6b78a499b57:hello world
    // f4b691d1-e8fc-4054-8fbc-60f26ff12dd4:hello world
    // 07ed4c4f-9635-4331-8a55-4aff144ab0a3:hello world
    // e150dbce-efdc-4f42-8225-fd70892481fd:hello world
    // 4f9fdacc-d7f7-4120-b186-2afc7ba118d5:hello world
    // 5a068314-965a-4c93-acc2-545f1c4a9610:hello world
    // 6e163eb0-c50a-4541-8030-66cd894c3a42:hello world
    // 29dcffcd-19bc-484b-bb0e-698e7a2cd9e1:hello world
    // 61c16eec-1094-4075-81ef-8460bc78694b:hello world
    // f7ad3b1b-4959-4b6f-9364-78fce33f7f34:hello world
    // 0e719113-a373-42cd-b4fb-112fc6cb807f:hello world
    // 286c3cd6-74c3-4410-aa53-86191934a305:hello world
    // 63087b6a-f7e9-4477-8c5d-20b4b6abe495:hello world
    // a95faa9b-a1b6-4afb-96f7-4a09c5f2a543:hello world
    // a585e10b-ca7e-4f48-a3a5-1ddbd23ae476:hello world
    // 61e06b52-1805-452d-853c-42e2e757f7a9:hello world
    // 6a61f9c5-f125-4513-9168-ec3589ad1d43:hello world
    // e152147a-b16c-4ff5-952c-2ecdecda1536:hello world
    // c7d54b7f-5bab-47b9-9eed-41f8084a4058:hello world
    // d07128df-4f16-425f-a86e-9eb7aa716d23:hello world
    // 346fc00c-6ad1-428c-9487-e48925c9bc8f:hello world
    // c672a8eb-658c-4b12-938c-12b54a4aaeb3:hello world
    // 27e0e93f-dc70-49b9-a606-a1e34e647a9c:hello world
    // 3073121c-9100-45b4-a889-e0ef0bf5b858:hello world
    // 615c906b-1281-4cf0-bffd-a1de81be1024:hello world
    // 7fbbab53-2706-4fa0-a00d-abb7968589eb:hello world
    // 3ca13ad7-4f5a-49f4-a1f5-355a558dc2dd:hello world
    // c0a11540-79e9-4165-8f3b-141fbab12c99:hello world
    // 1bd0480f-8dcb-458e-ae44-e74435622436:hello world
    // a023d679-13b6-48d2-9e6a-fce4b2ac8d4a:hello world
    // 1704d0ab-dc01-48e2-9704-11e3760ee869:hello world
    // 6f38f342-0b01-4df5-aead-b2e31473b237:hello world
    // 4552c7c6-08c1-4197-b0f5-ab88a05ce6f1:hello world
    // df9786e6-416d-413b-83b2-481fd80d4e33:hello world
    // 5a42cf31-db95-44cf-b1ba-964a65032ab5:hello world
    // 899da3a5-e130-420e-93e1-aa69904313b3:hello world
    // 2dd6d576-70bc-4ae9-9f2f-e394be274949:hello world
    // cac94b42-b686-4216-bfd9-27b3ab5db5a7:hello world
    // 850a2f9b-0b37-4218-92d2-e836fb70f3bc:hello world
    // 2147652b-7670-4793-ad20-41b24a1b992a:hello world
    // b07b9652-5731-44bf-9b85-0c9ef6f52ddb:hello world
    // b0df2f02-a571-46f0-abae-1339ee9abf35:hello world
    // fa3415b1-3868-4d90-bbf1-3f2081341350:hello world
    // 46300b86-faf8-4a42-90eb-b87b9714c06b:hello world
    // a7edad6d-3d75-4c79-9d4e-9c4809692a87:hello world
    // 983db306-5b70-4acc-aa75-c5a0356938b3:hello world
    // e0717024-ea23-4ad1-a58c-495f4bc462ba:hello world
    // 96345c54-768b-44d7-95c0-942bfc4c5310:hello world
    // d939ed64-e7c4-4429-9247-2099334a58d1:hello world
    // 9e81eaf2-54ad-4d16-afaf-5fdc8e9fe8ae:hello world
    // 8263983c-1233-42d8-b486-6fcfa6ca6b7e:hello world
    // 8e2c35e2-6152-46d2-8044-7ca5d0caf7cb:hello world
    // 40c87cef-f8a4-4b3d-8103-01717c91ef37:hello world
    // bc3aa8d1-4731-46e0-8e2d-64ef37ac73e6:hello world
    // ebfbf54c-c07f-4213-aeb9-30e0f22f29a8:hello world
    // c88a0863-8a67-4e4e-b4ea-10bf46351a71:hello world
    // a45e51d2-e1bc-494d-9e30-77e25d1eb44b:hello world
    // 9922d97f-17ad-44df-a1f1-633abafc6401:hello world
    // df7c5c4c-3d52-492f-8b08-0ae2da8988d2:hello world
    // a105219d-da2f-45b2-bba6-89f551831b69:hello world
    // d38c70a5-974f-49f3-96a7-9f2deb359f7e:hello world
    // aee1118f-2d3d-4b63-9154-a32044647409:hello world
    // f151420c-972d-4f10-80b1-a48b2b879669:hello world
    // 07705266-d300-4f18-964b-9899920959b8:hello world
    // ba18dbc0-82a1-4538-b1df-f2b31a8d808d:hello world
    // 7df7737f-229f-486c-bbfe-54f0f3bde44c:hello world
    // 9be0e8d3-38cb-40e6-8d9e-4b2062c993df:hello world
    // 04ba1079-cdd9-42ad-b1eb-4251771ecac1:hello world
    // 9dfb3f05-063c-4c6a-86d2-3882d306f46f:hello world
    // bb30581e-428f-4284-917a-d663f939dbac:hello world
    // 32cd3b54-b461-40d3-9080-5c6ca129f0b1:hello world
    // 3cf77080-b0b3-46bc-8600-64f35e2d91d3:hello world
    // ba17e77b-4adf-47cf-857e-d80910117d59:hello world
    // ffc886d9-49c3-440d-a010-586b5d11d389:hello world
    // bfb9877d-02e1-4ba7-b6c5-9b7fbe1c73f4:hello world
    // 4586df25-13c2-4d42-90d8-80751a625cc2:hello world
    // 99950a0d-ee4c-4c51-8b1a-0aac6d18cdf0:hello world
    // 51e1adc8-b4b3-4835-9896-8633bfba944e:hello world
    // 8be51307-d9d7-4535-8ba1-3e3b3b3ee229:hello world
    // 646eec3f-5b00-4561-9c98-a83d0a50dda7:hello world
    // 93494f62-3e51-44f5-92d4-5c85b34db809:hello world
    // 39b43d9b-8149-4064-8622-0260a77c1184:hello world
    // 29d4b568-c210-4a72-8d31-fe1eff98affc:hello world
    // 40809dbf-dcf7-492f-9873-144ca83390cd:hello world
    // 3534d0ac-8508-4a1a-8217-56895eb4df96:hello world
    // 1fbb6e9d-e4a3-4633-812d-49b07cc84bad:hello world
    // e6be2e00-fda8-423e-97d3-654222fd5cbb:hello world
    // 7973f444-83af-4e81-b3a1-2a18a16cc1ce:hello world
    // 50f95fbc-7ae1-4744-8f5a-ca805b2c725a:hello world
    // f73c67dd-6ae8-486e-a0b9-69f36443b2b7:hello world
    // b4e60c9d-332c-4223-aa87-fd86ed93386d:hello world
    // d9cc6ed0-009a-4ec5-a96b-88533f4a5377:hello world
    // c296e3df-fd34-4b9d-813b-6eeebd81adc5:hello world
    // ee284941-51c5-46b6-80c0-2bd0e6c7ccfb:hello world
    // a17ce229-f0bf-4489-90d5-27e98807df39:hello world
    // 027a0b1b-2034-43cd-9eda-5edc3643cbd4:hello world
    // 8e41a453-e7fa-451f-ba23-263201d42c6b:hello world
    // ac8e2bc7-93ef-4345-aa41-4438bea825ac:hello world

    init_file("./log4rs.yml", Default::default())?;



    let default_route = warp::any().and(warp::path::full()).map(|path: warp::path::FullPath|{
        let last_path = path.as_str().to_string();
        format!("Hello world:{}",last_path)
    });

    let init_route = warp::path("init").map(|| format!("ok"));



    let filter = init_route.or(default_route);


    let (tx,mut rx) = tokio::sync::mpsc::unbounded_channel();
    let (_, server) = warp::serve(filter)
    .bind_with_graceful_shutdown(([0, 0, 0, 0], 8080), async move {
        info!("rust server started");
        rx.recv().await;
        info!("rust servert receive close signal");
     });
    // Spawn the server into a runtime
    let thread = tokio::spawn(server);
    let mut signals = Signals::new(&[SIGTERM])?;
    let tx_signal = tx.clone();
    std::thread::spawn(move || {
        for _ in signals.forever() {
            let _ = tx_signal.send(()).unwrap();
            info!("start to close server");
            break;
        }
    });
    thread.await?;
    Ok(())
}
